package frame.options.simulation.create.nodes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.SystemColor;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionListener;

import model.algorithms.nodesMapping.NodesMappingAlgorithm;

@SuppressWarnings("serial")
public class NodesPanel extends JPanel
{
	private JList< NodesMappingAlgorithm > nodesMappingAlgorithmsList;
	public JList< NodesMappingAlgorithm > getNodesMappingAlgorithmsList(){ return nodesMappingAlgorithmsList; }
	public void setNodesMappingAlgorithmsList( JList< NodesMappingAlgorithm > nodesMappingAlgorithmsList ){ this.nodesMappingAlgorithmsList = nodesMappingAlgorithmsList; }
	
	public NodesPanel( List< NodesMappingAlgorithm > nodesMappingAlgorithms )
    {
    	DefaultListModel< NodesMappingAlgorithm > nodesMappingAlgorithmsListModel = new DefaultListModel< NodesMappingAlgorithm >();
    	
    	for( NodesMappingAlgorithm nodesMappingAlgorithm: nodesMappingAlgorithms )
			nodesMappingAlgorithmsListModel.addElement( nodesMappingAlgorithm );
    	
    	this.setNodesMappingAlgorithmsList( new JList< NodesMappingAlgorithm >( nodesMappingAlgorithmsListModel ) );
		this.getNodesMappingAlgorithmsList().setName( "availableList" );
		this.getNodesMappingAlgorithmsList().setCellRenderer( new NodesListCellRenderer() );
		
    	this.setLayout( new java.awt.BorderLayout() );
        this.add( titlePanel(), BorderLayout.NORTH );
        this.add( contentPanel(), BorderLayout.CENTER );
    }
	
    private JPanel titlePanel()
    {
    	JLabel titleLabel = new JLabel( "Virtual node mapping algorithm" );
        titleLabel.setFont( new Font( "MS Sans Serif", Font.BOLD, 14 ) );
        titleLabel.setBorder( new EmptyBorder( new Insets( 10, 10, 10, 10 ) ) );
        titleLabel.setOpaque( true );    
        
        JSeparator separator = new JSeparator();
        
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout( new java.awt.BorderLayout() );
        titlePanel.setBackground( Color.gray );
        titlePanel.add( titleLabel, BorderLayout.CENTER );
        titlePanel.add( separator, BorderLayout.SOUTH );
        
        return titlePanel;
    }
    
    private JPanel contentPanel()
	{
		JPanel contentPanel = new JPanel();
		contentPanel.setBorder( new EmptyBorder( new Insets( 50, 50, 50, 50 ) ) );
    	contentPanel.add( availablePanel() );
        
        return contentPanel;
	}
    
	private JPanel availablePanel()
	{
		JLabel availableLabel = new JLabel( "Available" );
        availableLabel.setFont( new Font( "MS Sans Serif", Font.BOLD, 12 ) );
        availableLabel.setAlignmentX( CENTER_ALIGNMENT );
		
		JScrollPane availableScrollPane = new JScrollPane( this.getNodesMappingAlgorithmsList() );
		availableScrollPane.setPreferredSize( new Dimension( 200, 250 ) );
		availableScrollPane.setBorder( BorderFactory.createLineBorder( SystemColor.activeCaption, 2 ) );
		
		JPanel availablePanel = new JPanel();
        availablePanel.setLayout( new BoxLayout( availablePanel, BoxLayout.PAGE_AXIS ) );
		availablePanel.add( availableLabel );
        availablePanel.add( Box.createRigidArea( new Dimension( 0, 5 ) ) );
		availablePanel.add( availableScrollPane );
		
		return availablePanel;
	}
	
	public void addListeners( ListSelectionListener listSelectionListener )
	{
		this.getNodesMappingAlgorithmsList().addListSelectionListener( listSelectionListener );
	}
	
	public boolean isOnlyOneSelected()
	{
		return this.getNodesMappingAlgorithmsList().getSelectedValuesList().size() == 1;
	}
	
	public NodesMappingAlgorithm getSelectedNodesMappingAlgorithm()
	{
		if( this.getNodesMappingAlgorithmsList().getSelectedValuesList().size() != 1 )	return null;
		return	this.getNodesMappingAlgorithmsList().getSelectedValue();
	}
	
}
